
#ifndef COMM_H_
#define COMM_H_

#include "stdint.h"

#define MAX_DATALEN     22

#define TRANSMITTED             0
#define TRANSMITTING            1

#define COMMTIMEOUT							0xFFFF

typedef struct
{
    uint8_t frame_data[MAX_DATALEN+6];
    uint8_t frame_str1;
    uint8_t frame_str2;
    uint8_t command;
    uint8_t datalen;
    uint8_t data[MAX_DATALEN];
    uint16_t checksum;
    uint8_t trans_status;
    uint8_t index;
    uint16_t wCrc16Cla;
    uint16_t wCrc16Get;
} COMM_t;

extern COMM_t Tx_Buff;
extern COMM_t Rx_Buff;

extern COMM_t RS485_Tx_Buff;
extern COMM_t RS485_Rx_Buff;


uint8_t Comm_ReadChar(uint8_t *ch);
uint8_t Comm_WriteBuff(uint8_t *pu8Buffer,uint32_t u32Count);

void CommPoll(void);
void CommApp(void);

extern void Set485TX(void);
extern void Set485RX(void);

extern void RS485_CommPoll(void);

extern void RS485EncoderTX(void);

#define CMD_CODE_VERSION                 0x01

#define CMD_READ_DEFAULTCALDATA                   0x0A
#define CMD_READ_CALDATA                   0x0B
#define CMD_UPDATA_CALDATA                 0x0C
#define CMD_WRITE_CALDATA                 0x0D
#define CMD_VERIFY_RAMCALDATA                 0x0E
#define CMD_VERIFY_ROMCALDATA                 0x0F

#define CMD_READ_MONITOR_DATA1            0x10

#define CMD_BOOT_GET_CODE                0xB0
#define CMD_BOOT_VERIFY                0xB1
#define CMD_BOOT_ERASE                 0xB2
#define CMD_BOOT_WRITE                 0xB3
#define CMD_BOOT_CHECKSUM              0xB4
#define CMD_BOOT_RESET                 0xB5

#define CMD_POS_REF_VALUE                 0xC0
#define CMD_SPD_REF_VALUE              0xC1
#define CMD_IQ_REF_VALUE                 0xC2
#define CMD_MOTION_MODE                0xC3
#define CMD_READ_RUNDATA                0xC8

#define CMD_ENCODER_ZERO_ADJUST                 0xCD
#define CMD_READ_ENCODER_ZERO                0xCE
#define CMD_CHECKSUM_ERROR               0xE0
#define CMD_INVALID_COMMAND              0xE1
#define CMD_SOFT_VERSION                 0x02
#define CMD_GET_ENCODER_DATA                 0x03
#define CMD_STOP_MOTOR                   0xCF
#define CMD_SYSTEM_RESET                 0xC4

typedef struct
{

    uint8_t  rxbuff[12];

    uint32_t  rxindex;

    uint32_t  rxcount;

    uint32_t  ReadABSErrorCNT;

    uint8_t   RunMode;

} RS485_ENCODER_PARAMTER;


extern RS485_ENCODER_PARAMTER stRS485_Encoder;

extern void sio_uart_buff_test(void);
extern void RS485_CommApp(void);
extern void RS485_CommLostMessageTX(void);
extern void RS485_FaultSendRun(void);
void CommPoll(void);
void RS485_CommLostMessageTX(void);

#endif

